package com.example.hospitalmanagement.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.bean.MedicalInsurancePolicyItem

/**
 * 医保政策中条目所使用的适配器
 */
class MedicalInsurancePolicyActivityAdapter(context: Context) :
    RecyclerView.Adapter<MedicalInsurancePolicyActivityAdapter.PolicyHolder>() {
    private val context = context
    lateinit var dataList: MutableList<MedicalInsurancePolicyItem>
    private lateinit var listener: (position: Int) -> Unit
    fun setListener(listener: (position: Int) -> Unit) {
        this.listener = listener
    }

    class PolicyHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val policyText: TextView = itemView.findViewById(R.id.policy_text)
        val policyDate: TextView = itemView.findViewById(R.id.policy_date)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PolicyHolder {
        val policyHolder = PolicyHolder(
            LayoutInflater.from(context)
                .inflate(R.layout.medical_insurance_policy_items, parent, false)
        )
        policyHolder.itemView.setOnClickListener {
            listener(policyHolder.adapterPosition)
        }
        return policyHolder
    }

    override fun onBindViewHolder(holder: PolicyHolder, position: Int) {
        holder.policyText.text = dataList[position].itemText
        holder.policyDate.text = dataList[position].itemDate
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}